<?php
/**
 * @title 2048. 下一个更大的数值平衡数
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param Integer $n
     * @return Integer
     */
    function nextBeautifulNumber($n) {
        /**
         * @since 2023-12-09 平衡数
         *
         * 位数 | 数字
         *  1     1
         *  2     2,2
         *  3     1,2,2 | 3,3,3
         *  4     1,3,3,3 | 4,4,4,4
         *  5     1,4,4,4,4 | 2,2,3,3,3 | 5,5,5,5,5
         *  6     1,2,2,3,3,3 | 1,5,5,5,5,5 | 2,2,4,4,4,4 | 6,6,6,6,6,6
         *  7     1,2,2,4,4,4,4
         */

        /**
         * @since 2023-12-09 多少位平衡数，穷列
         */
        $lenNumArray = [
            1 => [1],
            2 => [22],
            3 => [122, 212, 221, 333],
            4 => [1333, 3133, 3313, 3331, 4444],
            5 => [14444,22333,23233,23323,23332,32233,32323,32332,33223,33232,33322,41444,44144,44414,44441,55555],
            6 => [122333,123233,123323,123332,132233,132323,132332,133223,133232,133322,155555,212333,213233,213323,213332,221333,223133,223313,223331,224444,231233,231323,231332,232133,232313,232331,233123,233132,233213,233231,233312,233321,242444,244244,244424,244442,312233,312323,312332,313223,313232,313322,321233,321323,321332,322133,322313,322331,323123,323132,323213,323231,323312,323321,331223,331232,331322,332123,332132,332213,332231,332312,332321,333122,333212,333221,422444,424244,424424,424442,442244,442424,442442,444224,444242,444422,515555,551555,555155,555515,555551,666666],
            7 => [1224444],
        ];

        /**
         * @since 2023-12-09 获取5位数值平衡数
         */
        // $lenNumArray[5] = self::Len5Nums();
        // echo "[", implode(",", $lenNumArray[5]), "],", PHP_EOL;die();

        /**
         * @since 2023-12-09 获取6位数值平衡数
         */
        // $lenNumArray[6] = self::Len6Nums();
        // echo "[", implode(",", $lenNumArray[6]), "],", PHP_EOL;die();

        /**
         * @since 2023-12-09 n是几位数
         */
        $nString = strval($n);
        $nLen = strlen($nString);

        /**
         * @since 2023-12-09 遍历$nLen位数值平衡数，二分不至于
         */
        foreach ($lenNumArray[$nLen] as $num){
            if($num > $n){
                /**
                 * @return
                 */
                return $num;
            } else {}
        }

        /**
         * @since 2023-12-09 $nLen无法找到>$n的平衡数，返回$nLen+1最小平衡数
         * @return
         */
        return $lenNumArray[$nLen+1][0];
    }

    /**
     * @title 返回5位数值平衡数
     * @author start2004
     * @since 2023-12-09 Created
     *
     * @return int[]
     */
    function Len5Nums(){
        /**
         * @since 2023-12-09 5位的情况，1,4,4,4,4 | 2,2,3,3,3 | 5,5,5,5,5
         * @since 2023-12-09 增加6位的2,3
         */
        $lenArray = [14444, 41444, 44144, 44414, 44441, 55555];
        $num = "33333";
        $len = strlen($num);

        /**
         * @since 2023-12-09 第1个2出现的位置0到4
         */
        for($i=0; $i<$len; $i++){
            $num1 = $num;
            $num1[$i] = "2";

            /**
             * @since 2023-12-09 第2个2出现的位置
             */
            for($j=$i+1; $j<$len; $j++){
                $num2 = $num1;
                $num2[$j] = "2";

                $lenArray[] = intval($num2);
            }
        }

        /**
         * @since 2023-12-09 升序
         */
        sort($lenArray);

        /**
         * @return
         */
        return $lenArray;
    }

    /**
     * @title 返回6位数值平衡数
     * @author start2004
     * @since 2023-12-09 Created
     *
     * @return int[]
     */
    function Len6Nums(){
        /**
         * @since 2023-12-09 6位的情况，1,2,2,3,3,3 | 1,5,5,5,5,5 | 2,2,4,4,4,4 | 6,6,6,6,6,6
         * @since 2023-12-09 增加6位的1,2,3
         */
        $lenArray = [155555, 515555, 551555, 555155, 555515, 555551, 666666];
        $num = "333333";
        $len = strlen($num);

        /**
         * @since 2023-12-09 第1个1出现的位置从0到5
         */
        for($i=0; $i<$len; $i++){
            $num1 = $num;
            $num1[$i] = "1";

            /**
             * @since 2023-12-09 第1个2出现的位置从0到5，不和1重叠
             */
            for($j=0; $j<$len; $j++){
                if($j == $i){
                    continue;
                } else {}
                $num2 = $num1;
                $num2[$j] = "2";

                /**
                 * @since 2023-12-09 第2个2出现的位置
                 */
                for($n=$j+1; $n<$len; $n++){
                    if($n == $i){
                        continue;
                    } else {}

                    $num3 = $num2;
                    $num3[$n] = "2";

                    $lenArray[] = intval($num3);
                }
            }
        }

        /**
         * @since 2023-12-09 增加6位的2,4
         */
        $num = "444444";

        /**
         * @since 2023-12-09 第1个2出现的位置0到5
         */
        for($i=0; $i<$len; $i++){
            $num1 = $num;
            $num1[$i] = "2";

            /**
             * @since 2023-12-09 第2个2出现的位置
             */
            for($j=$i+1; $j<$len; $j++){
                $num2 = $num1;
                $num2[$j] = "2";

                $lenArray[] = intval($num2);
            }
        }

        /**
         * @since 2023-12-09 升序
         */
        sort($lenArray);

        /**
         * @return
         */
        return $lenArray;
    }
}


/**
 * @url http://127.0.0.1/leetcode/202312/2023.12.09.php
 */
$datas = [
    [1], // 22
    [1000], // 1333
    [3000], // 3133
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    $result = $obj->nextBeautifulNumber(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
